package admin.code.generator.core.metadata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import admin.code.generator.common.ConstantTemplateKey;
import admin.code.generator.common.DataTypeConverter;
import admin.code.generator.util.ClassNameMaker;

/**
*
* @author dw_chenqi
* @date 2016年3月2日下午5:31:23
*/
public class BaseMetaData {
	protected String packageName;//包名
	//private String className;//类名
	//private String domain;// domain 名称

	private String IdType;// id 类型
	private String mappingVal;// mapping vlaue
	private List<FieldProperty> properties;//所有属性

	private String domainName;
	private String controllerName;
	private String serviceName;
	private String serviceImplName;
	private String daoName;
	private String daoImplName;

	public BaseMetaData(String basePackage, TableMetaData tmd) {
		this.packageName = basePackage;
		fromTableMetaData(tmd);
	}

	public void fromTableMetaData(TableMetaData tmd) {
		//BaseMetaData bmd = new BaseMetaData() ;
		
		this.setIdType(DataTypeConverter.getJavaDataType(tmd.getIdType()));

		ClassNameMaker nameMaker = new ClassNameMaker(tmd.getTableName());
		this.setDomainName(nameMaker.makeClassName());
		this.setControllerName(nameMaker.makeControllerClassName());
		this.setMappingVal(nameMaker.controllerMappingVlaue());
		this.setServiceName(nameMaker.makeServiceClassName());
		this.setServiceImplName(nameMaker.makeServiceImplClassName());
		this.setDaoName(nameMaker.makeDaoClassName());
		this.setDaoImplName(nameMaker.makeDaoImpClassName());

		List<FieldProperty> props = new ArrayList<FieldProperty>();
		for (ColumnMetaData cmd : tmd.getColumns()) {
			//类型转换
			String javaDataType = DataTypeConverter.getJavaDataType(cmd.getType());
			props.add(new FieldProperty(cmd.getName(), javaDataType, cmd.getComment(), cmd.isPrimaryKey()));
		}
		this.setProperties(props);
	}

	public Map fillTempateModelData() {
		Map root = new HashMap(16);
		// 类名+包名
		root.put(ConstantTemplateKey.PACKAGE, this.getPackageName());
		root.put(ConstantTemplateKey.DOMAIN_NAME, this.getDomainName());
		root.put(ConstantTemplateKey.CONTROLLER_NAME, this.getControllerName());
		root.put(ConstantTemplateKey.SERVICE_NAME, this.getServiceName());
		root.put(ConstantTemplateKey.SERVICEIMPL_NAME, this.getServiceImplName());
		root.put(ConstantTemplateKey.DAO_NAME, this.getDaoName());
		root.put(ConstantTemplateKey.DAOIMPL_NAME, this.getDaoImplName());
		root.put(ConstantTemplateKey.ID_TYPE, this.getIdType());
		root.put(ConstantTemplateKey.PROPERTIES, this.getProperties());
		//
		root.put(ConstantTemplateKey.REQUEST_MAPPING_VALUE, this.getMappingVal());

		return root;
	}

	public List<FieldProperty> getProperties() {
		return properties;
	}

	public void setProperties(List<FieldProperty> properties) {
		this.properties = properties;
	}

	//	public String getDomain() {
	//		return domain;
	//	}
	//
	//	public void setDomain(String domain) {
	//		this.domain = domain;
	//	}

	public String getIdType() {
		return IdType;
	}

	public void setIdType(String idType) {
		IdType = idType;
	}

	public String getMappingVal() {
		return mappingVal;
	}

	public void setMappingVal(String mappingVal) {
		this.mappingVal = mappingVal;
	}

	public String getDomainName() {
		return domainName;
	}

	public void setDomainName(String domainName) {
		this.domainName = domainName;
	}

	public String getControllerName() {
		return controllerName;
	}

	public void setControllerName(String controllerName) {
		this.controllerName = controllerName;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getServiceImplName() {
		return serviceImplName;
	}

	public void setServiceImplName(String serviceImplName) {
		this.serviceImplName = serviceImplName;
	}

	public String getDaoName() {
		return daoName;
	}

	public void setDaoName(String daoName) {
		this.daoName = daoName;
	}

	public String getDaoImplName() {
		return daoImplName;
	}

	public void setDaoImplName(String daoImplName) {
		this.daoImplName = daoImplName;
	}

	/*public void setClassName(String className) {
		this.className = className;
	}
	
	public String getClassName() {
		return className;
	}
	*/
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getPackageName() {
		return packageName;
	}

}
